package com.mc.moring.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.gson.Gson;
import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseActivity;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.bean.AppConfigBean;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.bean.getWebLinkConfigBean;
import com.mc.moring.information.CapitalInforActivity;
import com.mc.moring.login.modle.LoginModle;
import com.mc.moring.main.WebViewActivity;
import com.mc.moring.main.modle.MianModle;
import com.mc.moring.util.AppManager;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.IntentUtils;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.SPUtils;
import com.mc.moring.util.SharePreUtil;
import com.mc.moring.util.T;
import com.mc.moring.util.ToastUtils;
import com.mc.moring.widge.CommonRetentionDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends BaseActivity {

    TextView back;
    TextView right_text;

    EditText et_password;
    ImageView iv_look_up;
    EditText et_password_confirm;
    ImageView iv_look_up_confirm;
    TextView tv_button;
    TextView tv_runtime_error;
    View register_line1;
    View register_line2;

    EditText et_invitation_code;
    CheckBox cb_1;
    TextView tv_1, tv_3;

    CheckBox cb_2;
    TextView tv_2;

    private String password = "";
    private String password_confirm = "";

    private String invita_code = "";
    private boolean look_up_confirm = false;
    private boolean look_up = false;
    String account = "";
    String code = "";
    LoginModle loginModle = null;

    private String isPass = "";

    private boolean isCheck = true;

//    private boolean firstCompare = true;

    private getWebLinkConfigBean urlBean;
    private AppConfigBean appConfigBean;

    CommonRetentionDialog retentionDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.logE("register page");
        setContentView(R.layout.activity_register);
        PointMgr.INSTANCE.sendEvent("cl_reunter_step_8_open");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_reunter_step_8_open");
        PointMgr.INSTANCE.sendEvent("cl_reunter_step_16_open");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_reunter_step_16_open");
        Intent intent = getIntent();
        if (intent != null) {
            String ss = intent.getStringExtra("account");
            String code = intent.getStringExtra("code");
            if (ss != null) {
                account = ss;
            }
            if (code != null) {
                this.code = code;
            }
        }
        loginModle = LoginModle.Companion.getInstant();
        init();

    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        back = findViewById(R.id.back);
        right_text = findViewById(R.id.right_text);
        et_password = findViewById(R.id.et_password);
        iv_look_up = findViewById(R.id.iv_look_up);
        et_password_confirm = findViewById(R.id.et_password_confirm);
        iv_look_up_confirm = findViewById(R.id.iv_look_up_confirm);
        tv_button = findViewById(R.id.tv_button);
        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
        tv_runtime_error = findViewById(R.id.tv_runtime_error);
        register_line1 = findViewById(R.id.register_line1);
        register_line2 = findViewById(R.id.register_line2);

        et_invitation_code = findViewById(R.id.et_invitation_code);
        cb_1 = findViewById(R.id.cb_1);
        tv_1 = findViewById(R.id.tv_1);
        tv_3 = findViewById(R.id.tv_3);


        retentionDialog = new CommonRetentionDialog(this);

        setLengthLimit(et_password, 4, 20);
        setLengthLimit(et_password_confirm, 4, 20);
        cb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
//                    isPass = 1;
                    isCheck = true;
                    if (TextUtils.isEmpty(et_password_confirm.getText().toString().trim()) || TextUtils.isEmpty(et_password.getText().toString().trim())) {
                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                        return;
                    }
                    tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
                    Log.e("----isCheck2",String.valueOf(isCheck));
                } else {
//                    isPass = 0;
                    isCheck = false;
                    if (TextUtils.isEmpty(et_password_confirm.getText().toString().trim()) || TextUtils.isEmpty(et_password.getText().toString().trim())) {
                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                        return;
                    }
                    tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    Log.e("----isCheck3",String.valueOf(isCheck));
                }
            }
        });
        cb_2 = findViewById(R.id.cb_2);
        tv_2 = findViewById(R.id.tv_2);

        tv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWebLinkConfigBean urlBean = new Gson().fromJson(SharePreUtil.getStringData("_getWebLinkConfig"),getWebLinkConfigBean.class);
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, WebViewActivity.class);
                intent.putExtra("url", urlBean.getData().getTermUse());
                Log.e("----link", GsonUtils.INSTANCE.getGson().toJson(urlBean.getData()));
                startActivity(intent);
            }
        });
        tv_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWebLinkConfigBean urlBean = new Gson().fromJson(SharePreUtil.getStringData("_getWebLinkConfig"),getWebLinkConfigBean.class);
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, WebViewActivity.class);
                intent.putExtra("url", urlBean.getData().getPrivacyAgree());
                startActivity(intent);
            }
        });

        right_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showHelp();

            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        retentionDialog.setDialogViewOnClickListener(new CommonRetentionDialog.DialogViewOnClickListener() {
            @Override
            public void onClickConfirm() {
            }

            @Override
            public void onClickCancel() {
                finish();
            }
        });
        tv_button.setOnClickListener(new View.OnClickListener() {//判断逻辑改为这里处理,要注意显示是否正确，以及修改时提示是否去掉,和协议勾选框是否有逻辑错误
            @Override
            public void onClick(View v) {
                LogUtils.logE(String.valueOf(isCheck));
                LogUtils.logE(isPass);
                if ( !isCheck ) return;
                switch ( isPass ){
                    case "NO_ENOUGH_LENGTH":
                        tv_runtime_error.setVisibility(View.VISIBLE);
                        tv_runtime_error.setText("Menos de 4 dígitos");
                        register_line1.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        register_line2.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        break;
                    case "ERROR_PASSWORD":
                        tv_runtime_error.setVisibility(View.VISIBLE);
                        tv_runtime_error.setText("Formato erróneo");
                        register_line1.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        register_line2.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        break;
                    case "NO_SAME":
                        tv_runtime_error.setVisibility(View.VISIBLE);
                        tv_runtime_error.setText("La contraseña de entrada es diferente.");
                        register_line1.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        register_line2.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        break;
                    case "SUCCESS":
                        tv_runtime_error.setVisibility(View.GONE);
                        register_line1.setBackgroundColor(Color.parseColor("#33000000"));
                        register_line2.setBackgroundColor(Color.parseColor("#33000000"));

                        invita_code = et_invitation_code.getText().toString();
                        if (!cb_1.isChecked()) {
                            T.showShort(getActivity(), "Agreement not agreed");
                            return;
                        }

                        SharedPreferences.Editor saveMobile = getSharedPreferences("personalMobile", Context.MODE_PRIVATE).edit();
                        saveMobile.putString("mobile", account);
                        saveMobile.apply();

                        Map<String, String> params = new HashMap<String, String>();
                        params.put("country", "56");
                        params.put("mobile", account);
                        params.put("smsCode", code);
                        params.put("password", password);
                        params.put("rePassword", password_confirm);
                        params.put("inviteCode", invita_code);
                        params.put("channel", "");
                        params.put("registerChannel", "");
                        params.put("latitude", "");
                        params.put("longitude", "");
                        params.put("fillInInfoGroup", "");
                        LogUtils.logE("params>" + GsonUtils.INSTANCE.getGson().toJson(params));
                        PointMgr.INSTANCE.sendEvent("cl_reunter_step_13_open");
                        Log.e("PointMgr.INSTANCE.sendEvent","cl_reunter_step_13_open");
                        showLoading();
                        loginModle.accountZiLiPageRegister(params, new HttpCallback() {
                            @Override
                            public void onSuccess(@NonNull String data) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject jsonObject = new JSONObject(data);
                                            if (loginModle.isSuccess(jsonObject)) {
                                                PointMgr.INSTANCE.sendEvent("cl_reunter_step_14_open");
                                                Log.e("PointMgr.INSTANCE.sendEvent","cl_reunter_step_14_open");
                                                JSONObject jsonData = jsonObject.optJSONObject("data");
                                                if (jsonData != null) {
                                                    ZiLiPageUserDataBean userDataBean = new ZiLiPageUserDataBean();
                                                    userDataBean.setData(jsonData, RegisterActivity.this);
                                                    SPUtils.INSTANCE.setPhone(jsonData.optString("mobile"));
                                                    ZiLiPageUserDataBean.setInstance(userDataBean);
                                                    if (TextUtils.equals("Y", userDataBean.getRewrite())) {
                                                        Intent intent = new Intent();
                                                        intent.setClass(RegisterActivity.this, CapitalInforActivity.class);
                                                        intent.putExtra("_from", "register");
                                                        startActivity(intent);
                                                    }
//                                            startActivity(MainActivity.class);
                                                    AppManager.getAppManager().finishActivity(LoginActivity.class);
                                                    AppManager.getAppManager().finishActivity(VerifyActivity.class);
                                                    finish();
                                                }
                                            } else {
                                                PointMgr.INSTANCE.sendEvent("cl_reunter_step_15_open");
                                                Log.e("PointMgr.INSTANCE.sendEvent","cl_reunter_step_15_open");
                                                T.showShort(RegisterActivity.this, loginModle.getStringToJson(jsonObject, "msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                                hideLoading();
                            }

                            @Override
                            public void onFaill(@NonNull String e) {
                                T.showShort(RegisterActivity.this, e);
                                hideLoading();
                            }
                        });
                        break;
                    default:
                        break;
                }
            }
        });
        iv_look_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_password.getText().toString().trim())) return;
                look_up = !look_up;
                if (look_up) {
                    iv_look_up.setImageResource(R.drawable.icon_look_down);
                    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    iv_look_up.setImageResource(R.drawable.icon_look_up);
                    et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }

            }
        });
        iv_look_up_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_password_confirm.getText().toString().trim())) return;
                look_up_confirm = !look_up_confirm;
                if (look_up_confirm) {
                    iv_look_up_confirm.setImageResource(R.drawable.icon_look_down);
                    et_password_confirm.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    iv_look_up_confirm.setImageResource(R.drawable.icon_look_up);
                    et_password_confirm.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });
        et_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    PointMgr.INSTANCE.sendEvent("cl_reunter_step_10_open");
                    Log.e("PointMgr.INSTANCE.sendEvent", "cl_reunter_step_10_open");
                }
            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {

                password = s.toString();
                tv_runtime_error.setVisibility(View.GONE);
                register_line1.setBackgroundColor(Color.parseColor("#33000000"));
                register_line2.setBackgroundColor(Color.parseColor("#33000000"));

                if (TextUtils.isEmpty(password_confirm)) {
                    return;
                }

                if (!verificationPwdLength(password)) {
                    isPass = "NO_ENOUGH_LENGTH";
                    tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    return;
                }

                if (hasChinese(password) || !verificationPwd(password)) {
                    isPass = "ERROR_PASSWORD";
                    tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    return;
                }

                if (!TextUtils.equals(password_confirm, password)) {
                    isPass = "NO_SAME";
//                    if ( firstCompare ) {
//                        firstCompare = false;
//                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
//                    } else {
//                        if ( isCheck ){
//                            tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
//                        } else {
//                            tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
//                        }
//                    }
                    if ( isCheck ){
                        tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
                    } else {
                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    }
                } else {
                    isPass = "SUCCESS";
                    tv_runtime_error.setVisibility(View.GONE);
                    register_line1.setBackgroundColor(Color.parseColor("#33000000"));
                    register_line2.setBackgroundColor(Color.parseColor("#33000000"));
                    Log.e("----isCheck",String.valueOf(isCheck));
                    if ( isCheck ){
                        tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
                    } else {
                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    }
                }

            }
        });

        et_password_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_reunter_step_11_open");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_reunter_step_11_open");
            }
        });
        et_invitation_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_reunter_step_12_open");
                Log.e("PointMgr.INSTANCE.sendEvent","cl_reunter_step_12_open");
            }
        });
        et_password_confirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                password_confirm = s.toString();
                tv_runtime_error.setVisibility(View.GONE);
                register_line1.setBackgroundColor(Color.parseColor("#33000000"));
                register_line2.setBackgroundColor(Color.parseColor("#33000000"));

                if (!verificationPwdLength(password_confirm)) {
//                    isPass = 0;
                    isPass = "NO_ENOUGH_LENGTH";
                    tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
//                    tv_runtime_error.setVisibility(View.VISIBLE);
//                    tv_runtime_error.setText("Menos de 4 dígitos");
                    return;
                }

                if (hasChinese(password_confirm) || !verificationPwd(password_confirm)) {
//                    isPass = 0;
                    isPass = "ERROR_PASSWORD";
                    tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
//                    tv_runtime_error.setVisibility(View.VISIBLE);
//                    tv_runtime_error.setText("Formato erróneo");
                    return;
                }


                if (!TextUtils.equals(password_confirm, password)) {
//                    isPass = 0;
                    isPass = "NO_SAME";
//                    tv_runtime_error.setVisibility(View.VISIBLE);
//                    tv_runtime_error.setText("La contraseña de entrada es diferente.");
//                    if ( firstCompare ) {
//                        firstCompare = false;
//                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
//                    } else {
//                        if ( isCheck ){
//                            tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
//                        } else {
//                            tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
//                        }
//
//                    }
                    if ( isCheck ){
                        tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
                    } else {
                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    }
                } else {
//                    isPass = 1;
                    isPass = "SUCCESS";
                    tv_runtime_error.setVisibility(View.GONE);
                    register_line1.setBackgroundColor(Color.parseColor("#33000000"));
                    register_line2.setBackgroundColor(Color.parseColor("#33000000"));
                    if ( isCheck ){
                        tv_button.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
                    } else {
                        tv_button.setBackgroundResource(R.drawable.bg_f4f4f4_corner_8);
                    }
                }

            }
        });

    }

    @Override
    public void initData() {
        urlBean = new Gson().fromJson(SharePreUtil.getStringData("_getWebLinkConfig"),getWebLinkConfigBean.class);
        Map<String, String> params = new HashMap<>();
        showLoading();
        MianModle.Companion.getInstant().securitygetApplicationConfig(params, new HttpCallback(){

            @Override
            public void onFaill(@NonNull String e) {
                hideLoading();
            }

            @Override
            public void onSuccess(@NonNull String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    if (MianModle.Companion.getInstant().isSuccess(jsonObject)){
                        appConfigBean = GsonUtils.INSTANCE.getGson().fromJson(data, AppConfigBean.class);

                    }
                } catch (Exception e){
                    e.printStackTrace();
                }
                hideLoading();
            }
        });
    }


    private SpannableString getSpannableString1() {
        String keyword1 = "Términos";
        String contentText = "Estoy de acuerdo con los Términos y la Política de Privacidad";
        int indexKeyword1Start = contentText.indexOf(keyword1);
        int indexKeyword1End = indexKeyword1Start + keyword1.length();
        SpannableString spannableString = new SpannableString(contentText);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                String androidButtonUrl = "https://www.baidu.com";
                if (!TextUtils.isEmpty(androidButtonUrl)) {
                    IntentUtils.startBrowser(RegisterActivity.this, androidButtonUrl);
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(RegisterActivity.this.getColor(R.color.blue_00daf7));
                ds.setUnderlineText(false);
            }
        }, indexKeyword1Start, indexKeyword1End, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        String keyword2 = "Política de Privacidad";
        int indexKeyword2Start = contentText.indexOf(keyword2);
        int indexKeyword2End = indexKeyword2Start + keyword2.length();

        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                String androidButtonUrl = "https://www.baidu.com";
                if (!TextUtils.isEmpty(androidButtonUrl)) {
                    IntentUtils.startBrowser(RegisterActivity.this, androidButtonUrl);
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(RegisterActivity.this.getColor(R.color.blue_00daf7));
                ds.setUnderlineText(false);
            }
        }, indexKeyword2Start, indexKeyword2End, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        return spannableString;
    }

    private void showHelp() {
        closeIme();
        View view = View.inflate(this, R.layout.popowin_help_list, null);
        PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.showAtLocation(right_text, Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.5f;
        getWindow().setAttributes(lp);
        TextView tv_tv1 = view.findViewById(R.id.tv_tv1);
        TextView tv_tv2 = view.findViewById(R.id.tv_tv2);
        TextView tv_tv3 = view.findViewById(R.id.tv_tv3);
        TextView tv_tv4 = view.findViewById(R.id.tv_tv4);
        TextView tv_tv5 = view.findViewById(R.id.tv_tv5);
        tv_tv1.setOnClickListener(v -> {
            popupWindow.dismiss();
            Intent intent = new Intent();
            intent.setClass(this, WebViewActivity.class);
            intent.putExtra("url", urlBean.getData().getFacebookUrl());
            startActivity(intent);
        });
        tv_tv2.setOnClickListener(v -> {
            popupWindow.dismiss();
            String phoneNumber = appConfigBean.getData().getWhatApp();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:"+phoneNumber));
            startActivity(intent);

        });
        tv_tv3.setOnClickListener(v -> {
            popupWindow.dismiss();
            String phoneNumber = appConfigBean.getData().getContactNumber();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:"+phoneNumber));
            startActivity(intent);

        });
        tv_tv4.setOnClickListener(v -> {
            popupWindow.dismiss();
            try {
                IntentUtils.openMail(this, appConfigBean.getData().getContactEmail());
            } catch (Exception e) {
                e.printStackTrace();
                ToastUtils.showLong(this, "No hay aplicación de buzón");
            }

        });
        tv_tv5.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(this, WebViewActivity.class);
            intent.putExtra("url", urlBean.getData().getFeedbackUrl());
            startActivity(intent);
            popupWindow.dismiss();
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });
    }

    private void backgroundAlpha(float f) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = f;
        getWindow().setAttributes(lp);
    }

    public static boolean hasChinese(String input) {
        String regex = "[\u4e00-\u9fa5]";
        return Pattern.matches(".*" + regex + ".*", input);
    }

    private void setLengthLimit(EditText editText, int min, int max) {
        InputFilter[] filters = new InputFilter[]{new LengthFilter(min, max)};
        editText.setFilters(filters);
    }

    public boolean verificationPwd(String password){
        if(password.length()>20 || password.length()<4){
            return false;
        }
        Pattern pattern = Pattern.compile("[a-zA-Z0-9\\S]+");//改为可以输入字符
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

    public boolean verificationPwdLength(String password){
        if(password.length()<4){
            return false;
        }
        return true;
    }

    public class LengthFilter implements InputFilter {
        private final int minLength;
        private final int maxLength;

        public LengthFilter(int minLength, int maxLength) {
            this.minLength = minLength;
            this.maxLength = maxLength;
        }

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            int keep = maxLength - (dest.length() - (dend - dstart));

            if (keep <= 0) {
                return "";
            } else if (keep >= end - start) {
                return null;
            } else {
                keep += start;
                if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        }
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        retentionDialog.showToRetentionDialog(RegisterActivity.this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View currentFocus = getCurrentFocus();
            if (currentFocus instanceof EditText) {
                EditText editText = (EditText) currentFocus;
                if (!isInSide(editText, ev) && isSoftInPutDisplayed()) {
                    closeIme();
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private Boolean isInSide(View currentFocus, MotionEvent ev) {
        int[] location = {0,0};
        currentFocus.getLocationInWindow(location);
        int left = location[0];
        int top = location[1];
        int right = left + currentFocus.getWidth();
        int bottom = top + currentFocus.getHeight();
        return (ev.getX() >= left && ev.getX() <= right && ev.getY() > top && ev.getY() < bottom);

    }

    private Boolean isSoftInPutDisplayed() {
        WindowInsetsCompat insetsCompat = ViewCompat.getRootWindowInsets(getWindow().getDecorView());
        return insetsCompat != null && insetsCompat.isVisible(WindowInsetsCompat.Type.ime());
    }

    private void closeIme() {
        if (getActivity().getCurrentFocus() == null) {
            return;
        }

        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public void onStop(){
        super.onStop();
        hideLoading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PointMgr.INSTANCE.sendEvent("cl_reunter_step_9_open");
        Log.e("PointMgr.INSTANCE.sendEvent", "cl_reunter_step_9_open");
        PointMgr.INSTANCE.sendEvent("cl_reunter_step_17_open");
        Log.e("PointMgr.INSTANCE.sendEvent", "cl_reunter_step_17_open");
    }
}
